package com.web_bloks.types.client;

import com.web_bloks.system.client.Logger;

import com.google.gwt.json.client.JSONParser;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.json.client.JSONString;
import com.google.gwt.json.client.JSONValue;

import java.lang.String;
import java.util.Set;
import java.util.Iterator;

/**
 * SimpleMap
 * 	key ==> value
 * 
 * 
 * 	public String	getKey(String key);
 * 	public void		setKey(String key, String value);
 * 	public boolean	containsKey(String key);
 * 	public void		clear();
 * 
 * @author Jean-Lou Dupont
 *
 */
public class SimpleMap 
	implements SimpleIterator, SimpleSerializer {

	JSONObject obj;
	
	Set<String> keyset = null;
	Iterator<String> iterator = null;
	String currentKey = null;
	
	/*===================================================================
	 * CONSTRUCTORS
	 ===================================================================*/
	
	public SimpleMap() {
		super();
		obj = new JSONObject();
	}
	
	public SimpleMap(JSONObject object) {
		super();
		obj = object;
	}
	
	/*===================================================================
	 * Map
	 ===================================================================*/
	/**
	 * Uses 'stringValue' to return a 'real' Java string
	 * and not a JSONString.
	 */
	public String getKey(String key) {
		JSONValue v;
		try {
			v = obj.get(key);
			if (null == v)
				return new String();
			
		} catch(Exception e) {
			Logger.logWarn("SimpleMap.getKey: tried accessing key["+key+"]");
			return new String();
		}
		return v.isString().stringValue();
	}
	public void setKey(String key, String value) {
		obj.put(key, new JSONString(value));
	}

	public boolean containsKey(String key) {
		return obj.containsKey(key);
	}
	public void clear() {
		obj = null;
		obj = new JSONObject();
	}
	
	/*===================================================================
	 * SimpleIterator
	 ===================================================================*/

	public void reset() {
		keyset = null; //helps GC ?
		keyset = obj.keySet();
		iterator = keyset.iterator();
	}
	
	public boolean hasNext() {
		return iterator.hasNext();
	}
	
	public String nextString() {
		String key = (String) iterator.next();
		JSONValue value = obj.get(key);
		return value.toString();
	}

	public String[] nextStringPair() {
		String key = (String) iterator.next();
		JSONValue jsvalue = obj.get(key);
		JSONString value = jsvalue.isString();
		return new String[]{key, value.stringValue()};
	}
	/*===================================================================
	 * SimpleSerializer
	 ===================================================================*/
	
	public String serialize() {
		return obj.toString();
	}
	
	public static SimpleMap fromSerializedString(String serialized) {
		JSONValue value = JSONParser.parse(serialized);
		
		if (value.isObject() == null) {
			Logger.logError("SimpleMap.fromSerializedString: EXCEPTION raised");
			throw new RuntimeException("SimpleMap.fromSerializedString: not an object");
		}
		
		return new SimpleMap( value.isObject() );
	}
	
}//END